<?php
require_once('IncludeManager.php');
package('junction_class');
package('junction_sql');
package('junction_sql_expression');

$widgetTable 	= new Junction_Sql_Table('widgets');
$widgetClass 	= new Junction_Class_Class('widget', 'id', $widgetTable);
$widgetClass->addField('id', new Junction_Sql_Column('widget_id', $widgetTable));
$widgetClass->addField('name', new Junction_Sql_Column('widget_name', $widgetTable));

$operandNode = createSimpleOperand($widgetClass);

echo Junction_Sql_Expression_Builder::buildSelectSql($widgetClass, $operandNode);
echo '<br /><br />';

$operandNode = createComplexOperand($widgetClass);

echo Junction_Sql_Expression_Builder::buildSelectSql($widgetClass, $operandNode);
echo '<br /><br />';

function createSimpleOperand($widgetClass)
{
	$node1 = new Junction_Sql_Expression_OperandNode(
		$widgetClass->getField('id')
	);	

	$node2 = new Junction_Sql_Expression_OperandNode(
		new Junction_Sql_Scalar(1)
	);

	$node3 = new Junction_Sql_Expression_OperatorNode(
		$node1, 
		new Junction_Sql_Operator(Junction_Sql_Operator::EQUAL_SYMBOL), 
		$node2
	);	
	
	return $node3;
}


function createComplexOperand($widgetClass)
{
	$node1 = new Junction_Sql_Expression_OperandNode(
		$widgetClass->getField('id')
	);	

	$node2 = new Junction_Sql_Expression_OperandNode(
		new Junction_Sql_Scalar(1)
	);
	
	$node3 = new Junction_Sql_Expression_OperandNode(
		$widgetClass->getField('name')
	);	

	$node4 = new Junction_Sql_Expression_OperandNode(
		new Junction_Sql_Scalar('foo')
	);	
	
	$node5 = new Junction_Sql_Expression_OperatorNode(
		$node1, 
		new Junction_Sql_Operator(Junction_Sql_Operator::EQUAL_SYMBOL), 
		$node2
	);		
	
	$node6 = new Junction_Sql_Expression_OperatorNode(
		$node3, 
		new Junction_Sql_Operator(Junction_Sql_Operator::EQUAL_SYMBOL), 
		$node4
	);
	
	$node7 = new Junction_Sql_Expression_OperatorNode(
		$node5, 
		new Junction_Sql_Operator(Junction_Sql_Operator::AND_SYMBOL), 
		$node6
	);	
	
	return $node7;
}
?>

